/*
 *  AMPProxyObserverAMPProxyObserver.cpp
 *  AMPlugin
 *
 *  Created by Toan on 10/23/12.
 *  Copyright 2012 __MyCompanyName__. All rights reserved.
 *
 */

#include "VCPlugInHeaders.h"

// Interface includes:
#include "IPanelControlData.h"
#include "ISubject.h"

// General includes:
#include "CDialogObserver.h"
#include "IWidgetParent.h"
#include "IApplication.h"
#include "IDialogMgr.h"
#include "CoreResTypes.h"
#include "LocaleSetting.h"
#include "RsrcSpec.h"

// Project includes:
#include "AMPID.h"
#include "CDialogController.h"
#include "HTTPRequestHelper.h"
#include "JSONHelper.h"
#include "AMPLogDataRecord.h"
#include "AMGlobal.h"

class AMPProxyObserver : public CDialogObserver
	{
	public:
		/**
		 Constructor.
		 @param boss interface ptr from boss object on which this interface is aggregated.
		 */
		AMPProxyObserver(IPMUnknown* boss) : CDialogObserver(boss) {}
		
		/** Destructor. */
		virtual ~AMPProxyObserver() {}
		
		/** 
		 Called by the application to allow the observer to attach to the subjects 
		 to be observed, in this case the dialog's info button widget. If you want 
		 to observe other widgets on the dialog you could add them here. 
		 */
		virtual void AutoAttach();
		
		/** Called by the application to allow the observer to detach from the subjects being observed. */
		virtual void AutoDetach();
		
		/**
		 Called by the host when the observed object changes, in this case when
		 the dialog's info button is clicked.
		 @param theChange specifies the class ID of the change to the subject. Frequently this is a command ID.
		 @param theSubject points to the ISubject interface for the subject that has changed.
		 @param protocol specifies the ID of the changed interface on the subject boss.
		 @param changedBy points to additional data about the change. 
		 Often this pointer indicates the class ID of the command that has caused the change.
		 */
		virtual void Update
		(
		 const ClassID& theChange, 
		 ISubject* theSubject, 
		 const PMIID& protocol, 
		 void* changedBy
		 );
	private:
		void DoCloseDialog(int32 nTypeDialog);
		
		IDialog* DoOpenDialog(int32 nTypeDialog);
	};


/* CREATE_PMINTERFACE
 Binds the C++ implementation class onto its 
 ImplementationID making the C++ code callable by the 
 application.
 */
CREATE_PMINTERFACE(AMPProxyObserver, kAMPProxyObserverImpl)

/********************************************************************************************************************
 *****Function: AutoAttach
 *****Creator:  Toan Nguyen
 *****IN: 
 *****out: 
 *****Objective: 
 ********************************************************************************************************************/
void AMPProxyObserver::AutoAttach()
{
	// Call the base class AutoAttach() function so that default behavior
	// will still occur (OK and Cancel buttons, etc.).
	CDialogObserver::AutoAttach();
	
	do
	{
		// Get the IPanelControlData interface for the dialog:
		InterfacePtr<IPanelControlData> panelControlData(this, UseDefaultIID());
		if (panelControlData == nil)
		{
			ASSERT_FAIL("AMPProxyObserver::AutoAttach() panelControlData invalid");
			break;
		}
		
		AttachToWidget(kAMPRadioAutoWidgetID, IID_ITRISTATECONTROLDATA, panelControlData);
		
		AttachToWidget(kAMPRadioManualWidgetID, IID_ITRISTATECONTROLDATA, panelControlData);
		
		AttachToWidget(kAMPCheckboxEnablePassWidgetID, IID_ITRISTATECONTROLDATA, panelControlData);
		
		AttachToWidget(kCancelButton_WidgetID, IID_IBOOLEANCONTROLDATA, panelControlData);
		
	} while (false);
}

/********************************************************************************************************************
 *****Function: AutoDetach
 *****Creator:  Toan Nguyen
 *****IN: 
 *****out: 
 *****Objective: 
 ********************************************************************************************************************/
void AMPProxyObserver::AutoDetach()
{
	// Call base class AutoDetach() so that default behavior will occur (OK and Cancel buttons, etc.).
	CDialogObserver::AutoDetach();
	
	do
	{
		// Get the IPanelControlData interface for the dialog:
		InterfacePtr<IPanelControlData> panelControlData(this, UseDefaultIID());
		if (panelControlData == nil)
		{
			ASSERT_FAIL("AMPProxyObserver::AutoDetach() panelControlData invalid");
			break;
		}
		
		DetachFromWidget(kAMPRadioAutoWidgetID, IID_ITRISTATECONTROLDATA, panelControlData);
		
		DetachFromWidget(kAMPRadioManualWidgetID, IID_ITRISTATECONTROLDATA, panelControlData);
		
		DetachFromWidget(kAMPCheckboxEnablePassWidgetID, IID_ITRISTATECONTROLDATA, panelControlData);
		
		DetachFromWidget(kCancelButton_WidgetID, IID_IBOOLEANCONTROLDATA, panelControlData);
	} while (false);
}

/********************************************************************************************************************
 *****Function: Update
 *****Creator:  Toan Nguyen
 *****IN: 
 *****out: 
 *****Objective: 
 ********************************************************************************************************************/
void AMPProxyObserver::Update
(
 const ClassID& theChange, 
 ISubject* theSubject, 
 const PMIID &protocol, 
 void* changedBy
)
{
	// Call the base class Update function so that default behavior will still occur (OK and Cancel buttons, etc.).
	CDialogObserver::Update(theChange, theSubject, protocol, changedBy);
	
	do
	{
		InterfacePtr<IControlView> controlView(theSubject, UseDefaultIID());
		if (controlView == nil)
		{
			ASSERT_FAIL("AMPProxyObserver::Update() controlView invalid");
			break;
		}
		
		// Get the button ID from the view.
		WidgetID theSelectedWidget = controlView->GetWidgetID();
		
		InterfacePtr<IWidgetParent> iWidgetParent(this, UseDefaultIID());
		if(!iWidgetParent)
			break;
		InterfacePtr<IPanelControlData>	iPanelControlData( 
														  (IPanelControlData*)iWidgetParent->QueryParentFor(IID_IPANELCONTROLDATA) );
		ASSERT(iPanelControlData);
		if(!iPanelControlData)
		{
			break;
		}
		// Toan added here to set proxy server
		if (theSelectedWidget == kAMPRadioAutoWidgetID && theChange == kTrueStateMessage)
		{
			////cout << "select auto mode" << endl;
			g_ProxyEnable = FALSE;
			
			IControlView* proxyPortView = iPanelControlData->FindWidget(kAMPProxyGroupAllWidgetID);
			if (proxyPortView == nil)
			{
				break;
			}
			proxyPortView->Disable();
			break;
		}
		
		if (theSelectedWidget == kAMPRadioManualWidgetID && theChange == kTrueStateMessage)
		{
			////cout << "select manual mode" << endl;
			g_ProxyEnable = TRUE;

			IControlView* proxyPortView = iPanelControlData->FindWidget(kAMPProxyGroupAllWidgetID);
			if (proxyPortView == nil)
			{
				break;
			}
			proxyPortView->Enable(TRUE, TRUE);
			break;
		}
		 
		IControlView* authenView = iPanelControlData->FindWidget(kAMPProxyGroupPassWidgetID);
		if (authenView == nil)
		{
			break;
		}
		
		if (theSelectedWidget == kAMPCheckboxEnablePassWidgetID)
		{
			if (theChange == kTrueStateMessage)
			{
				////cout << "check box enable" << endl;
				g_ProxyAuthEnable = TRUE;
				authenView->Enable(TRUE, TRUE);
				break;
				
			}else if (theChange == kFalseStateMessage)
			{
				////cout << "check box disable" << endl;
				g_ProxyAuthEnable = FALSE;
				authenView->Disable();
				break;
			}
		}
		
		if (theSelectedWidget == kCancelButton_WidgetID && theChange == kTrueStateMessage)
		{
			LoadSettingInfo();
		}
		
	} while (false);
}

/********************************************************************************************************************
 *****Function: DoCloseDialog
 *****Creator:  Toan Nguyen
 *****IN: int32 nTypeDialog
 *****out: 
 *****Objective: close dialog
 ********************************************************************************************************************/
void AMPProxyObserver::DoCloseDialog(int32 nTypeDialog)
{
	do
	{
		// Get the application interface and the DialogMgr.
		InterfacePtr<IWidgetParent> iWidgetParent(this, UseDefaultIID());
		if(!iWidgetParent)
			break;
		
		InterfacePtr<IDialog> iDialog((IDialog *) iWidgetParent->QueryParentFor(IID_IDIALOG));
		
		iDialog->Close();
	} while (false);
}

/********************************************************************************************************************
 *****Function: DoOpenDialog
 *****Creator:  Toan Nguyen
 *****IN: int32 nTypeDialog
 *****out: 
 *****Objective: open dialog
 ********************************************************************************************************************/
IDialog* AMPProxyObserver::DoOpenDialog(int32 nTypeDialog)
{
	
	do
	{
		int32	DialogResourceID;
		if (nTypeDialog == AMPluginCMSLoginAction)
		{
			DialogResourceID = kCMSLoginDialogResourceID;
		} 
		else if (nTypeDialog == AMPluginBrowserIssueAction)
		{
			DialogResourceID = kBscSlDlgIssuesDialogResourceID;
		}
		else if (nTypeDialog == kAMPLuginProgressAction)
		{
			DialogResourceID = kAMPProgressDlgResourceID;
		}
		else if (nTypeDialog == kAMPLuginProxyAction)
		{
			DialogResourceID = kAMPProxyDlgResourceID;
		}
		// Get the application interface and the DialogMgr.	
		InterfacePtr<IApplication> application(GetExecutionContextSession()->QueryApplication());
		if (application == nil)
		{
			ASSERT_FAIL("BscDlgActionComponent::DoDialog: application invalid");
			break;
		}
		InterfacePtr<IDialogMgr> dialogMgr(application, UseDefaultIID());
		if (dialogMgr == nil)
		{ 
			ASSERT_FAIL("BscDlgActionComponent::DoDialog: dialogMgr invalid"); 
			break;
		}
		
		// Load the plug-in's resource.
		PMLocaleId nLocale = LocaleSetting::GetLocale();
		RsrcSpec dialogSpec
		(
		 nLocale,					// Locale index from PMLocaleIDs.h. 
		 kAMPPluginID,			// Our Plug-in ID from BasicDialogID.h. 
		 kViewRsrcType,				// This is the kViewRsrcType.
		 DialogResourceID,			// Resource ID for our dialog.
		 kTrue						// Initially visible.
		 );
		
		// CreateNewDialog takes the dialogSpec created above, and also
		// the type of dialog being created (kMovableModal).
		IDialog* dialog = dialogMgr->CreateNewDialog(dialogSpec, IDialog::kMovableModal);
		if (dialog == nil)
		{ 
			ASSERT_FAIL("BscDlgActionComponent::DoDialog: can't create dialog"); 
			break;
		}
		
		// Open the dialog.
		dialog->Open(); 
		
		return dialog;
		
	} while (false);
	
	return NULL;
}
// End, AMPProxyObserver.cpp.




